/**
 * This function replaces the text in the phrases for an input field so the user can edit their value
 * it shows an edit button to confirm the edit.
 */
$(function()
{
	$('#detail').val('Game');
}
);
function startGame(){
	/*var sec = 5;
	var timer = setInterval(function() { 
	   $('#hideMsg span').text(sec--);
	   if (sec == 0) {
		  $('#hideMsg span').text('0');		  
		  clearInterval(timer);
		  endGame();
	   } 
	}, 1000);*/
	$('.start-game').css('display','block');
	if($('#startgame').val()==$('#playagaintext').val()){
		$('.start-game').css('display','none');
		$('#currentindex').css('display','none');
		$('#result-false').css('display','none');
		$('#result-true').css('display','none');
		window.location.reload(true);
	}
	$('#startbutton').css('display','none');
	$('#currentindex').css('display','block');
}
function showEdit(iId) {
    if ($('#actionPoint' + iId).is(':visible')) {
        $('#actionPoint' + iId).hide();
        $('#actionInput' + iId).show();
    }
    else {
        $('#actionPoint' + iId).show();
        $('#actionInput' + iId).hide();
    }

}

function getRandomQuestion(ignoreId,result){
	var homelink = $('#baseurl').html();
	$.ajax({ 
		url: homelink+"static/ajax.php",
		minLength: 1,
		dataType: "text",
		data: {
			'ignoreId' : ignoreId,
			'result':result,
			'core[ajax]': 'true',
			'core[call]':'answer.getRandomQuestion'
		},
		success: function( data ) {
			var arrData = data.split("!2-");
			var arrQuestion = arrData[0].split(","); 
			var arrRand = arrData[1].split("-");	
			$('.div-point').text(arrRand[1]); 
			if(arrRand[0]=='1'){
				var arrField0 = arrQuestion[0].split("!1-");
				$('#image-question').attr('src',arrField0[2]);
				$('.linktitlea').text(arrField0[1]);
				var arrField1= arrQuestion[1].split("!1-");
				$('.linktitleb').text(arrField1[1]);
				$('#result-a').text('1');
				$('#result-b').text('0');
			}
			if(arrRand[0]=='2'){				
				var arrField0 = arrQuestion[0].split("!1-");
				var arrField1= arrQuestion[1].split("!1-");
				$('#image-question').attr('src',arrField0[2]);
				$('.linktitlea').text(arrField1[1]);				
				$('.linktitleb').text(arrField0[1]);	
				$('#result-a').text('0');
				$('#result-b').text('1');
			}			
			$('#edit-question').attr('href','index.php?do=/answer/question/add/id_'+arrField0[0]);
			$('#starta').attr('onclick',"checkResult('A',"+arrField0[0]+")");
			$('#startb').attr('onclick',"checkResult('B',"+arrField0[0]+")");
			
					
		}
	  });
	  	  	
	/*$.ajaxCall('answer.getRandomQuestion','').success(function( data ) {
		alert(data)
    });*/
}

function endGame(){
	var homelink = $('#baseurl').html();
	$.ajax({ 
		url: homelink+"static/ajax.php",
		minLength: 1,
		dataType: "text",
		data: {
			'core[ajax]': 'true',
			'core[call]':'answer.endGame'
		},
		success: function( data ) {	
			alert("Complete Game with "+data+" points.");	
			window.location.reload(true);	
		}
	  });
}

function countDownGame(element,minutes,seconds) {	
	interval = setInterval(function() {
		var el = document.getElementById(element);
		if(seconds == 0) {
			if(minutes == 0) {
				el.innerHTML = "countdown's over!"; 
				endGame();                   
				clearInterval(interval);
				return;
			} else {
				minutes--;
				seconds = 60;
			}
		}
		if(minutes > 0) {
			var minute_text = minutes + (minutes > 1 ? ' minutes' : ' minute');
		} else {
			var minute_text = '';
		}
		var second_text = seconds > 1 ? 'seconds' : 'second';
		el.innerHTML = minute_text + ' ' + seconds + ' ' + second_text + ' remaining';
		seconds--;
	}, 1000);
}
sec = 7;
timer = 0;
function checkResult(type,ignoreId){
	//clearInterval(interval);
	//var interval;	
	//countDownGame('countDownGame',0,5);
	clearInterval(timer);
	$('#hideMsg span').text('7');	
	sec = 7;
	timer = setInterval(function() { 
	   $('#hideMsg span').text(sec--);
	   if (sec == 0) {
		  $('#hideMsg span').text('0');		  
		  clearInterval(timer);
		  endGame();
	   } 
	}, 1000);
	
	$('#mp3false audio').trigger('play');
	if(parseInt($('#iIndex').text()) <= parseInt($('#totalIndex').text()) ){
		$('#iIndex').text(parseInt($('#iIndex').text())+1);	
	}	
		
	if(type=='A'){
		var aResult = $('#result-a').html();
		if(parseInt(aResult)==0){
			endGame();
			$('#mp3false audio').trigger('play');
			$('#result-true').css('display','none');
			$('#result-false').css('display','block');
		}else{
			$('#mp3true audio').trigger('play');
			$('#result-true').css('display','block');
			$('#result-false').css('display','none');
		}
		getRandomQuestion(ignoreId,aResult);
	}
	if(type=='B'){
		var bResult = $('#result-b').html();
		if(parseInt(bResult)==0){
			endGame();
			$('#mp3false audio').trigger('play');
			$('#result-true').css('display','none');
			$('#result-false').css('display','block');
		}else{
			$('#mp3true audio').trigger('play');
			$('#result-true').css('display','block');
			$('#result-false').css('display','none');
		}
		getRandomQuestion(ignoreId,bResult);
	}	
	/*if(parseInt($('#iIndex').text()) > parseInt($('#totalIndex').text())){
		endGame();
		//window.location.reload(true);
	}*/
}

function checkResult1(type,iId,end,index){	
	if(parseInt(end)==1){
		$('#currentindex').css('display','block');
		$('.start-game').css('display','none');
		$('#startgame').val($('#playagaintext').val());		
	}else{
		$('#iIndex').html(parseInt(index)+2);
		$('#result-false').css('display','none');
		$('#result-true').css('display','none');
		
	}
	$('#startbutton').css('display','none');
	
	$.ajaxCall('answer.checkResult', 'id=' + iId + '&type=' + type + '&end='+end).success(function( data ) {
		var arrData = data.split('-');
		if(arrData[0]==1){
			//$('#result-true').css('display','block');
			$('#result-true').css({"display":"block","text-align":"center", "color":"#04cd1c", "text-transform":"uppercase"}); 
			$('#result-false').css('display','none');
			$('.info-'+index).css('display','none');
			$('.info-'+(index+1)).css('display','block');
			$('.div-point').html(arrData[1]);
		}else{
			if(arrData[0]==0){
				$('#result-false').css({"display":"block","text-align":"center","color":"#F00","text-transform":"uppercase"});	
				$('#result-true').css('display','none');
				$('.info-'+index).css('display','none');
				$('.info-'+(index+1)).css('display','block');				
			}else{
				$('#startbutton').css('display','block');
			}
		}
					
    });
	
    return true;
}
function updateAction(iId) {
    $.ajaxCall('answer.updatePoint', 'id=' + iId + '&point=' + $('#txtPoint' + iId).val());
    return true;
}

function showSubCategory(iId) {
    $.ajaxCall('answer.showSubCategory', 'id=' + iId);
    return true;
}
$Behavior.showSubCategory = function () {
    $('#parent_id').change(function () {
        $.ajaxCall('answer.showSubCategory', 'id=' + $('#parent_id').val());
        return true;
    });
};
$Behavior.redirectPage = function () {
    $('.selectSort').change(function() {
        location.href = $('.selectSort').val();
        return true;
    });
};
$Behavior.featuredQuestionSlide = function() {
    if ($('#allinone_contentSlider_common').size() > 0) {
        $('#allinone_contentSlider_common').allinone_contentSlider({
            skin: 'common',
            width: 520,
            height: 150,
            autoHideBottomNav: false,
            showPreviewThumbs: false,
            autoHideNavArrows: false,
            thumbsWrapperMarginBottom: 0
        });
    }
};
$Behavior.onLoadEvents = function(){
	$("#parent_id option[value='3']").attr('selected', 'selected');
}
